#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "sem.h"
#include "squeue.h"
#include "envdata.h"

extern int alarm_temp;
extern int semid;
extern void *shmaddr;
extern squeue *sq;
extern int haveData;
extern struct sembuf P,V;

/**
 *@breif:处理线程
 *@desc:将队列的队尾数据放入共享内存，队尾是最新接收的数据
 *@author：马德顺
 */
void *handler_proc(void * arg)
{
	int pos;
	int a,b,c,d,e;
	int i = 0;

	sleep(1);

	while(1)
	{
		if( haveData == 0 ) { sleep(1);continue; }
		semop(semid,&P,1);
		pos = sq->rear-1 < 0 ? 5 :sq->rear-1;

		//当光照强度大于设定值，就自动报警
		if( (sq->pData[pos]->temp[0]) > alarm_temp )
		{
			system("echo 1 > /sys/class/leds/led2/brightness");//开led2灯
			system("echo 1 > /sys/class/leds/led3/brightness");//开led3灯
		}

		memcpy(shmaddr, sq->pData[pos], sizeof(struct sendEnv));
		semop(semid,&V,1);
		sleep(1);

	}

	pthread_exit("tid exit");
	
}
